package cn.cena._02hello_work;

import cn.cena.util.ConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {
    // 队列名称
    public static final String QUEUE_WORK = "hello";
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = null;
        Channel channel = null;
        try {
            // 获取链接
            connection = ConnectionUtil.getConnection();

            // 创建与Exchang的通道
            channel = connection.createChannel();

            // 声明队列,参数1：队列名称；参数2：是否持久化；参数3：是否独占此连接；参数4：队列参数
            channel.queueDeclare(QUEUE_WORK, true, false, false, null);

            String msg = "你好，水电费";

            // 消息发布""表示默认交换机
            channel.basicPublish("", QUEUE_WORK, null, msg.getBytes());
            System.out.println("发布成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (channel != null){
                channel.close();
            }
            if (connection != null){
                connection.close();
            }
        }
    }
}
